package org.zfes.snowier.zbss.xzy.biz.config.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.zfes.snowier.core.data.DataSet;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.zfes.snowier.core.util.ZBeanUtil;
import org.zfes.snowier.core.util.ZDateUtil;
import org.zfes.snowier.core.util.ZAlert;
import org.zfes.snowier.core.util.ZAssert;
import org.zfes.snowier.core.data.ParamMap;

import com.google.common.collect.Lists;

import org.zfes.snowier.zbss.xzy.biz.config.dao.ConfigItemMapper;
import org.zfes.snowier.zbss.xzy.biz.config.model.ConfigItem;
import org.zfes.snowier.zbss.xzy.biz.config.service.IConfigItemService;

import java.util.stream.Stream;
@Service
public class ConfigItemServiceImpl implements IConfigItemService{
 	@Autowired
	private ConfigItemMapper aIConfigItemMapper;
	
	@Transactional(rollbackFor=Exception.class)
 	@Override
	public void createAIConfigItem(ConfigItem aIConfigItem) {
		ZBeanUtil.setCreateInfo(aIConfigItem);
		ZBeanUtil.validateBean(aIConfigItem);
		
		Object id1=aIConfigItemMapper.selectIdByCode(aIConfigItem.getCateId(),aIConfigItem.getCode());
		if(id1!=null){
			ZAlert.Error("配置编码已经存在");
		}
		aIConfigItemMapper.insertSelective(aIConfigItem);
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<ConfigItem> loadAIConfigItemById(Long id) {
		if(id!=null){
			return Optional.ofNullable(aIConfigItemMapper.selectByPrimaryKey(id));
		}
		return Optional.empty();
		
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void updateAIConfigItem(ConfigItem aIConfigItem) {
		ZBeanUtil.setUpdatedInfo(aIConfigItem);
		ZBeanUtil.validateBean(aIConfigItem);
		Object id1=aIConfigItemMapper.selectIdByCode(aIConfigItem.getCateId(),aIConfigItem.getCode());
		if(id1!=null&&!id1.equals(aIConfigItem.getId())){
			ZAlert.Error("配置编码已经存在");
		}
		aIConfigItemMapper.updateByPrimaryKeySelective(aIConfigItem);
	}
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void deleteAIConfigItem(Long id) {
		ZAssert.notNull(id, "请选择要操作的数据");
		aIConfigItemMapper.deleteByPrimaryKey(id);
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void setAIConfigItemStatus(Long id, Byte status) {
		ZAssert.notNull(id, "请选择要操作的数据");
		ZAssert.notNull(status, "状态参数错误");
		if(!Stream.of(new Byte[] {-1,0,1}).anyMatch(val->val.equals(status))) {
			ZAlert.serviceLogicalError("状态参数错误");
		}
		aIConfigItemMapper.updateStatus( id,  status) ;
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public DataSet loadAIConfigItemDataSet(Map<String, Object> params) {
		ParamMap pm=ParamMap.filterParam(params);
		pm.getObj("name").ifPresent(v->pm.updateParam("name", "%"+v+"%"));
		DataSet ds=DataSet.newDS2(aIConfigItemMapper.selectListCount(pm), aIConfigItemMapper.selectMapListPage(pm));
		return ds;
	}
	
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<ConfigItem> loadByCode(Long cateId,java.lang.String code){
		if(code!=null&&cateId!=null){
			return Optional.ofNullable(aIConfigItemMapper.selectByCode(cateId,code));
		}
		return Optional.empty();
	}
	@Override
	public List<ConfigItem> loadItemByCate(Long cateId, Byte status) {
		if(cateId==null) {
			return Lists.newArrayList();
		}
		
		return aIConfigItemMapper.selectByCateId( cateId,  status);
	}
}